﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using Startup.MVVM;
using Startup.Shell.Domain.FunctionalityContracts;

namespace Startup.Shell.Controls
{
	/// <summary>
	/// Interaction logic for CheckBoxList.xaml
	/// </summary>
	public partial class CheckBoxList
	{
		public static readonly DependencyProperty CheckedItemsProperty =
			DependencyProperty.Register("CheckedItems", typeof(ObservableCollection<CommandContractBase>), typeof(CheckBoxList), new PropertyMetadata(null, OnCheckedCollectionChanged));

		public static readonly DependencyProperty AllItemsProperty =
			DependencyProperty.Register("AllItems", typeof(IList<CommandContractBase>), typeof(CheckBoxList), new PropertyMetadata(null, OnAllItemsChanged));

		private static void OnAllItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			CheckBoxList target = (CheckBoxList)d;
			
			target.Items.Clear();

			foreach (var item in target.AllItems)
				target.Items.Add(new CheckedItem { Source = item });

			target.UpdateCheck();
		}

		public IList<CommandContractBase> AllItems
		{
			get { return (IList<CommandContractBase>)GetValue(AllItemsProperty); }
			set { SetValue(AllItemsProperty, value); }
		}

		private static void OnCheckedCollectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			CheckBoxList target = (CheckBoxList) d;
			target.UpdateCheck();
		}

		private void UpdateCheck()
		{
			if (CheckedItems == null)
				return;

			List<object> checkedItems = CheckedItems.OfType<object>().ToList();

			foreach (CheckedItem item in Items)
			{
				item.Checked = checkedItems.Contains(item.Source);
			}
		}

		public ObservableCollection<CommandContractBase> CheckedItems
		{
			get { return (ObservableCollection<CommandContractBase>)GetValue(CheckedItemsProperty); }
			set { SetValue(CheckedItemsProperty, value); }
		}

		public CheckBoxList()
		{
			InitializeComponent();
		}

		private void CheckBox_Checked(object sender, RoutedEventArgs e)
		{
			CheckBox box = (CheckBox) sender;
			CheckedItem item = (CheckedItem)box.Content;

			if (item.Checked && !CheckedItems.Contains(item.Source))
			{
				CheckedItems.Add((CommandContractBase)item.Source);
			}
			else if (!item.Checked && CheckedItems.Contains(item.Source))
			{
				CheckedItems.Remove((CommandContractBase)item.Source);
			}
		}
	}

	public class CheckedItem : ViewModelBase
	{
		private bool _checked;
		private object _source;

		public bool Checked
		{
			get { return _checked; }
			set
			{
				if (_checked == value)
					return;

				_checked = value;
				RaisePropertyChanged("Checked");
			}
		}

		public object Source
		{
			get { return _source; }
			set
			{
				if (_source == value)
					return;

				_source = value;
				RaisePropertyChanged("Source");
			}
		}

		public override string ToString()
		{
			return Source.ToString();
		}
	}
}
